import 'package:cached_network_image/cached_network_image.dart';
import 'package:fate_blind_box/ui/user/profile.dart';
import 'package:fate_blind_box/utils/common_widget.dart';
import 'package:flutter/material.dart';

import '../../../network/Api.dart';
import '../../../theme/theme_color.dart';
import '../../../utils/provider.dart';
import '../../../widget/icon_text.dart';

class activityListUI extends StatelessWidget {
  const activityListUI({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const _activityListWidget();
  }
}

class _activityListWidget extends StatefulWidget {
  const _activityListWidget({Key? key}) : super(key: key);

  @override
  State<_activityListWidget> createState() => _activityListState();
}

class _activityListState extends State<_activityListWidget> {

  final _listData = [];
  final int _page = 1; //加载的页数
  bool isLoading = false;
  final ScrollController _scrollController = ScrollController();

  @override
  Widget build(BuildContext context) {

    return AppWidget.navTitleBar(
      bgColor: const Color(0xFFF9F9F9),
      title:"活动管理",
      childWidget: _listData.isNotEmpty?RefreshIndicator(
        onRefresh: _onRefresh,
        child: ListView.builder(
          controller: _scrollController,
          itemBuilder: (_, index) {
            if (index == _listData.length) {
              //加载更多
              return _getMoreWidget();
            } else {
              if (_listData?.isNotEmpty == true) {
                return _buildItems(
                    _listData?[index], index);
              } else {
                return Container();
              }
            }
          },
          itemCount:_listData.length,
        ),
      ):gettype(),
    );
  }

  @override
  void initState() {
    super.initState();
    _getMeLookList(_page);
    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        _getMore();
      }
    });

    setState(() {});

  }

  _getMeLookList(int page) async {
    final res = await Api.getInstance().getReadMe(Provider.memberId);
    final data = res['data'];

    if (data != null) {
      List list = data;
      if (list.isNotEmpty) {
        _listData.clear();
        setState(() {
          _listData.addAll(list);
        });
      }
    }
  }

  @override
  _buildItems(Map map, int index) {
    double screenWidth = MediaQuery.of(context).size.width;
    return GestureDetector(
      onTap: () {

        // Navigator.of(context).push(MaterialPageRoute(
        //     builder: (builder) => UserProfileUi(memberId:map['id'])));
      },
      child: _listData.isNotEmpty
          ? Container(
        height: 192,
        margin: EdgeInsets.fromLTRB(14, 10, 14, 0),
        // margin: const EdgeInsets.symmetric(horizontal: 12,vertical: 10),
        decoration: BoxDecoration(
          color: Colors.white,
          border: Border(
              bottom: BorderSide(
                  color: ThemeColor.dividerColor, width: 1)),
        ),
        child: Column(
          // mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [

            Container(
              margin: EdgeInsets.only(left: 10,top: 13,bottom:10),
              child: Text(
                '创建日期 2023-06-21 14:0',
                style: TextStyle(
                    color: ThemeColor.textColorSecond,
                    fontSize: 12,
                    ),
              ),
            ),
            const Divider(height: 1,),
            Row(
              mainAxisAlignment: MainAxisAlignment.start,

              children: [
                Container(
                  margin: const EdgeInsets.only(left: 10,top: 13,bottom:10,right: 10),
                  child:
                  ClipRRect(
                    borderRadius: BorderRadius.circular(5),
                    child: CachedNetworkImage(
                      imageUrl: Provider.fixAvatar(map["profilePhoto"]),
                      width: 114,
                      height: 70,
                      fit: BoxFit.fill,
                    ),
                  ),
                ),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      // padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
                      height: 50,
                      width: screenWidth/7*4,
                      // padding: const EdgeInsets.symmetric(vertical: 10),
                      child: Text(
                        maxLines:2,
                        overflow:TextOverflow.ellipsis,
                        "活动名称活动名活动名活动名活动名",
                        style: TextStyle(
                          color: ThemeColor.textColor,
                          fontSize: 15,
                          fontWeight: FontWeight.w600,),
                      ),
                    ),
                    SizedBox(height: 5,),
                    Container(
                      // padding: const EdgeInsets.symmetric(vertical: 10),
                      child: Row(
                        children: [
                          Text(
                            overflow:TextOverflow.clip,
                            "24",
                            style: TextStyle(
                              color: ThemeColor.appColor,
                              fontSize: 12),
                          ),
                          Text(
                            overflow:TextOverflow.clip,
                            "浏览",
                            style: TextStyle(
                                color: ThemeColor.textColorSecond,
                                fontSize: 12),
                          ),
                          SizedBox(width: 10,),
                          Text(
                            overflow:TextOverflow.clip,
                            "12",
                            style: TextStyle(
                                color: ThemeColor.appColor,
                                fontSize: 12),
                          ),
                          Text(
                            overflow:TextOverflow.clip,
                            "报名",
                            style: TextStyle(
                                color: ThemeColor.textColorSecond,
                                fontSize: 12),
                          ),
                        ],
                      )
                    ),
                  ],
                )
              ],
            ),
            const Divider(height: 1,),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              // crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Container(
                  height: 32,
                  margin: const EdgeInsets.only(top: 8,left: 15),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(16),
                    border: Border.all(
                        width: 1,
                        style: BorderStyle.solid,
                        color: ThemeColor.sendMsgColor),
                  ),
                  alignment: Alignment.center,
                  padding: const EdgeInsets.symmetric(horizontal: 12),
                  child: Text('推广', style: const TextStyle(color: Colors.black,fontSize: 14),),
                ),
                Container(
                  height: 32,
                  margin: const EdgeInsets.only(top: 8),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(16),
                    border: Border.all(
                        width: 1,
                        style: BorderStyle.solid,
                        color: ThemeColor.sendMsgColor),
                  ),
                  alignment: Alignment.center,
                  padding: const EdgeInsets.symmetric(horizontal: 12),
                  child: Text('分享', style: const TextStyle(color: Colors.black,fontSize: 14),),
                ),
                Container(
                  height: 32,
                  margin: const EdgeInsets.only(top: 8),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(16),
                    border: Border.all(
                        width: 1,
                        style: BorderStyle.solid,
                        color: ThemeColor.sendMsgColor),
                  ),
                  alignment: Alignment.center,
                  padding: const EdgeInsets.symmetric(horizontal: 12),
                  child: Text('关闭', style: const TextStyle(color: Colors.black,fontSize: 14),),
                ),
                Container(
                  height: 32,
                  margin: const EdgeInsets.only(top: 8,right: 15),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(16),
                    border: Border.all(
                        width: 1,
                        style: BorderStyle.solid,
                        color: ThemeColor.sendMsgColor),
                  ),
                  alignment: Alignment.center,
                  padding: const EdgeInsets.symmetric(horizontal: 12),
                  child: const Text('删除', style: TextStyle(color: Colors.red,fontSize: 14),),
                ),
              ],
            )
          ],
        ),
      )
          : SizedBox(
        height: 500,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Image.asset(
              'images/ic_nofriend.png',
              width: 175,
              height: 122,
            ),
          ],
        ),
      ),
    );
  }

  /// 下拉刷新方法,为list重新赋值
  Future<void> _onRefresh() async {
    await Future.delayed(const Duration(seconds: 3), () {});
  }

  /// 上拉加载更多
  Future _getMore() async {
    if (!isLoading) {
      setState(() {
        isLoading = true;
      });
      await Future.delayed(const Duration(seconds: 1), () {});
    }
  }
  Widget _getMoreWidget() {
    return Center(
      child: Offstage(
        offstage: !isLoading,
        child: const Row(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Text(
              '加载中...',
              style: TextStyle(fontSize: 16.0),
            ),
            CircularProgressIndicator(
              strokeWidth: 1.0,
            )
          ],
        ),
      ),
    );
  }

  Widget gettype() {
    return SizedBox(
      height: 500,
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Image.asset(
              'images/MeLook.png',
              width: 175,
              height: 122,
            ),
            ElevatedButton(
              onPressed: (){
                Navigator.pop(context, 0);
              },
              child: Container(
                height: 38,
                width: 104,
                child: const Text('返回'),
                alignment: Alignment.center,
              ),
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.all(Colors.white),
                foregroundColor: MaterialStateProperty.all(Color(0xFF7F69D9)),
                elevation: MaterialStateProperty.all(0),
                side: MaterialStateProperty.all(const BorderSide(width: 1,color: Color(0xFF7F69D9))),//边框
                shape: MaterialStateProperty.all(
                    const StadiumBorder(
                        side: BorderSide(
                          //设置 界面效果
                          style: BorderStyle.solid,
                          color: Color(0xFF7F69D9),
                        )
                    )
                ),//圆
              ),
            ),
          ],
        ),
      ),
    );
  }
}

